/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.opensymphony.workflow.entity;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jackysoft.util.XmlUtils;


/**
 * 
 */


public class RegisterDescriptor extends AbstractDescriptor {
    //~ Instance fields ////////////////////////////////////////////////////////

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Map<String, String> args = new HashMap<String, String>();
    protected String type;
    protected String variableName;

    //~ Constructors ///////////////////////////////////////////////////////////

    /**
     * 
     */
    public RegisterDescriptor() {
    }

   
    /**
     * 
     */
    public RegisterDescriptor(org.dom4j.Element element) {
    	super(element);this.init(element);
    }

    
	
    public Map<String, String> getArgs() {
        return args;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setVariableName(String variableName) {
        this.variableName = variableName;
    }

    public String getVariableName() {
        return variableName;
    }

    public void writeXML(PrintWriter out, int indent) {
        XmlUtils.printIndent(out, indent++);
        out.println("<register " + (hasId() ? ("id=\"" + getId() + "\" ") : "") + "type=\"" + type + "\" variable-name=\"" + variableName + "\">");

        Iterator iter = args.entrySet().iterator();

        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            XmlUtils.printIndent(out, indent);
            out.print("<arg name=\"");
            out.print(entry.getKey());
            out.print("\">");

            if ("beanshell".equals(type) || "bsf".equals(type)) {
                out.print("<![CDATA[");
                out.print(entry.getValue());
                out.print("]]>");
            } else {
                out.print(XmlUtils.encode(entry.getValue()));
            }

            out.println("</arg>");
        }

        XmlUtils.printIndent(out, --indent);
        out.println("</register>");
    }

    
    protected void init(org.dom4j.Element register) {
    	this.type = register.attributeValue("type");
    	this.variableName = register.attributeValue("variable-name");
    	
    	try {
    		setId(Integer.parseInt(register.attributeValue("id")));
    	} catch (NumberFormatException e) {
    		
    	}  	
    	
    	for(Iterator argitr = register.elementIterator("arg");argitr.hasNext();){
    		org.dom4j.Element arg = (org.dom4j.Element) argitr.next();
    		this.args.put(arg.attributeValue("name"), arg.getText());
    	}
    	
    }
   	/**
	 * 
	 */
	public void setArgs(Map<String, String> args) {
		this.args = args;
	}
}
